<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class CommissionLedger extends Model
{
    use SoftDelete;

    protected $name = 'commission_ledger';

    protected $autoWriteTimestamp = 'datetime';

    protected $append = ['status_label'];

    public const STATUS_PENDING = 0;
    public const STATUS_SETTLED = 1;
    public const STATUS_PAID = 2;

    public static function statusMap(): array
    {
        return [
            self::STATUS_PENDING => '待结算',
            self::STATUS_SETTLED => '已结算',
            self::STATUS_PAID => '已支付',
        ];
    }

    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id');
    }

    public function orderItem()
    {
        return $this->belongsTo(OrderItem::class, 'order_item_id');
    }

    public function beneficiary()
    {
        return $this->belongsTo(User::class, 'beneficiary_id');
    }

    public function statement()
    {
        return $this->belongsTo(SellerStatement::class, 'statement_id');
    }

    public function getStatusLabelAttr(): string
    {
        return self::statusMap()[(int) $this->status] ?? '未知状态';
    }
}
